package com.zycfc.zsf.boot.util.bean;

import java.util.*;
import com.zycfc.zsf.boot.util.number.*;
import com.zycfc.zsf.boot.util.io.*;
import java.io.*;
import org.slf4j.*;

public abstract class PropertiesUtils
{
    private static final Logger LOGGER;
    
    public static Boolean getBoolean(final Properties p, final String name, final Boolean defaultValue) {
        return BooleanMoreUtils.toBooleanObject(p.getProperty(name), defaultValue);
    }
    
    public static Integer getInt(final Properties p, final String name, final Integer defaultValue) {
        return NumberMoreUtils.toIntObject(p.getProperty(name), defaultValue);
    }
    
    public static Long getLong(final Properties p, final String name, final Long defaultValue) {
        return NumberMoreUtils.toLongObject(p.getProperty(name), defaultValue);
    }
    
    public static Double getDouble(final Properties p, final String name, final Double defaultValue) {
        return NumberMoreUtils.toDoubleObject(p.getProperty(name), defaultValue);
    }
    
    public static String getString(final Properties p, final String name, final String defaultValue) {
        return p.getProperty(name, defaultValue);
    }
    
    public static Properties loadFromFile(final String generalPath) {
        final Properties p = new Properties();
        InputStream is = null;
        try {
            is = GeneralResourceUtils.asStream(generalPath);
            p.load(is);
        }
        catch (IOException e) {
            PropertiesUtils.LOGGER.warn("Load property from " + generalPath + " fail ", (Throwable)e);
        }
        finally {
            IOUtils.closeQuietly(is);
        }
        return p;
    }
    
    public static Properties loadFromString(final String content) {
        final Properties p = new Properties();
        final Reader reader = new StringReader(content);
        try {
            p.load(reader);
        }
        catch (IOException ex) {}
        finally {
            IOUtils.closeQuietly(reader);
        }
        return p;
    }
    
    static {
        LOGGER = LoggerFactory.getLogger((Class)PropertiesUtils.class);
    }
}
